package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.CourseVo;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 课程信息表(CourseVo)表服务实现类
 *
 * @author makejava
 * @since 2024-05-10 16:15:24
 */
@Service("courseService")
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CourseAttachmentMapper courseAttachmentMapper;
    @Resource
    private CourseTopicMapper courseTopicMapper;
    @Resource
    private AttachmentMapper attachmentMapper;
    @Resource
    private SysTopicMapper sysTopicMapper;

    @Resource
    private AttachmentTypeMapper attachmentTypeMapper;

    @Resource
    private TopicTypeMapper topicTypeMapper;

    @Resource
    private LessonMapper lessonMapper;
//    /**
//     * 通过ID查询单条数据
//     *
//     * @param courseId 主键
//     * @return 实例对象
//     */
//    @Override
//    public CourseVo queryById(Long courseId) {
//        Course course = courseMapper.queryById(courseId);
//        CourseVo courseVo = new CourseVo();
//        courseVo.setCourseId(course.getCourseId());
//        courseVo.setCourseName(course.getCourseName());
//        courseVo.setContent(course.getContent());
//        courseVo.setHeadImg(course.getHeadImg());
//        courseVo.setPeriods(course.getPeriods());
//
//        CourseAttachment courseAttachment = courseAttachmentMapper.queryById(course.getCourseId());
//        courseVo.setAttachmentId(courseAttachment.getAttachmentId());
//        Attachment attachment = attachmentMapper.queryById(courseAttachment.getAttachmentId());
//        courseVo.setAttachmentName(attachment.getAttachmentName());
//        courseVo.setAttachmenDescription(attachment.getDescribe());
//        courseVo.setAttachmentType(attachment.getAttachmentType());
//
//        CourseTopic courseTopic = courseTopicMapper.queryById(course.getCourseId());
//        courseVo.setTopicId(courseTopic.getTopicId());
//        SysTopic sysTopic = sysTopicMapper.queryById(courseTopic.getTopicId());
//        courseVo.setTopicId(courseTopic.getTopicId());
//        courseVo.setTopicName(sysTopic.getTopicName());
//        courseVo.setTopicType(sysTopic.getTopicType());
//        courseVo.setTopicDescribe(sysTopic.getDescribe());
//
//        return courseVo;
//    }


//    /**
//     * 分页查询
//     *
//     * @param course 筛选条件
//     * @return 查询结果
//     */
//    @Override
//    public List<CourseVo> queryList(Course course) {
//        List<Course> courses = courseMapper.queryAllByLimit(course);
//        List<CourseVo> courseVos = new ArrayList<>();
//
//
//        for (Course course1 : courses) {
//            CourseVo courseVo = new CourseVo();
//            BeanUtils.copyProperties(course1, courseVo);
//
//            // 附加其他属性
//            CourseAttachment courseAttachment = courseAttachmentMapper.queryById(course1.getCourseId());
//            if (courseAttachment != null) {
//                courseVo.setAttachmentId(courseAttachment.getAttachmentId());
//                Attachment attachment = attachmentMapper.queryById(courseAttachment.getAttachmentId());
//                if (attachment != null) {
//                    courseVo.setAttachmentName(attachment.getAttachmentName());
//                    courseVo.setAttachmenDescription(attachment.getDescribe());
//                    courseVo.setAttachmentType(attachment.getAttachmentType());
//                }
//            }
//
//            CourseTopic courseTopic = courseTopicMapper.queryById(course1.getCourseId());
//            if (courseTopic != null) {
//                courseVo.setTopicId(courseTopic.getTopicId());
//                SysTopic sysTopic = sysTopicMapper.queryById(courseTopic.getTopicId());
//                if (sysTopic != null) {
//                    courseVo.setTopicName(sysTopic.getTopicName());
//                    courseVo.setTopicType(sysTopic.getTopicType());
//                    courseVo.setTopicDescribe(sysTopic.getDescribe());
//                }
//            }
//
//            courseVos.add(courseVo);
//        }
//
//        return courseVos;
//    }

    @Override
    public Course queryById(Long courseId) {
        return this.courseMapper.queryById(courseId);
    }

    @Override
    public List<Lesson> queryByCourseId(Long courseId) {
        return this.lessonMapper.queryByCourseId(courseId);
    }

    @Override
    public List<Course> queryList(Course course) {
        List<Course> courses = courseMapper.queryAllByLimit(course);
        return courses;
    }

    /**
     * 新增数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Override
    public Course insert(Course course) {
        this.courseMapper.insert(course);
        insertCourseAttachment(course);
        insertAttachmentType(course);
        insertCourseTopic(course);
        insertTopicType(course);
        return course;
    }



    /**
     * 修改数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Transactional
    public Course update(Course course) {
        this.courseMapper.update(course);
        courseAttachmentMapper.deleteCourseAttachmentByCourseId(course.getCourseId());
        insertCourseAttachment(course);
        attachmentTypeMapper.deleteAttachmentTypeByCourseId(course.getCourseId());
        insertAttachmentType(course);
        courseTopicMapper.deleteCourseTopicByCourseId(course.getCourseId());
        insertCourseTopic(course);
        topicTypeMapper.deleteTopicTypeByCourseId(course.getCourseId());
        insertTopicType(course);
        return course;
    }

    @Override
    public int updateById(Long courseId, String headImg) {
        return courseMapper.updateById(courseId,headImg);
    }

    /**
     * 通过主键删除数据
     *
     * @param courseId 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(Long courseId) {
        courseAttachmentMapper.deleteCourseAttachmentByCourseId(courseId);
        attachmentTypeMapper.deleteAttachmentTypeByCourseId(courseId);
        courseTopicMapper.deleteCourseTopicByCourseId(courseId);
        topicTypeMapper.deleteTopicTypeByCourseId(courseId);
        return this.courseMapper.deleteById(courseId) > 0;
    }

    private void insertAttachmentType(Course course) {
        Long[] attachmentIds = course.getAttachmentIds();
        if (StringUtils.isNotEmpty(attachmentIds))
        {
            List<AttachmentType> list = new ArrayList<AttachmentType>();
            Set<String> set = new HashSet<String>();
            for (Long attachmentId : attachmentIds)
            {
                Attachment attachment = attachmentMapper.queryById(attachmentId);
                if (set.contains(attachment.getAttachmentType())){
                    continue;
                }
                AttachmentType attachmentType = new AttachmentType();
                attachmentType.setAttachmentType(attachment.getAttachmentType());
                attachmentType.setCourseId(course.getCourseId());
                list.add(attachmentType);
                set.add(attachment.getAttachmentType());
            }
            attachmentTypeMapper.batchAttachmentType(list);
        }
    }


    private void insertCourseAttachment(Course course) {
        Long[] attachmentIds = course.getAttachmentIds();
        if (StringUtils.isNotEmpty(attachmentIds))
        {
            List<CourseAttachment> list = new ArrayList<CourseAttachment>(attachmentIds.length);
            for (Long attachmentId : attachmentIds)
            {
                Attachment attachment = attachmentMapper.queryById(attachmentId);
                CourseAttachment courseAttachment = new CourseAttachment();
                courseAttachment.setCourseId(String.valueOf(course.getCourseId()));
                courseAttachment.setAttachmentId(attachmentId);
                courseAttachment.setAttachmentType(attachment.getAttachmentType());
                list.add(courseAttachment);
            }
            courseAttachmentMapper.batchCourseAttachment(list);
        }

    }

    private void insertTopicType(Course course) {
        Long[] topicIds = course.getTopicIds();
        if (StringUtils.isNotEmpty(topicIds))
        {
            List<TopicType> list = new ArrayList<TopicType>();
            Set<String> set = new HashSet<String>();
            for (Long topicId : topicIds)
            {
                SysTopic topic = sysTopicMapper.queryById(topicId);
                if (set.contains(topic.getTopicType())){
                    continue;
                }
                TopicType topicType = new TopicType();
                topicType.setTopicType(topic.getTopicType());
                topicType.setCourseId(course.getCourseId());
                list.add(topicType);
                set.add(topic.getTopicType());
            }
            topicTypeMapper.batchTopicType(list);
        }
    }
    private void insertCourseTopic(Course course) {
        Long[] topicIds = course.getTopicIds();
        if (StringUtils.isNotEmpty(topicIds))
        {
            List<CourseTopic> list = new ArrayList<CourseTopic>(topicIds.length);
            for (Long topicId :topicIds )
            {
                SysTopic sysTopic = sysTopicMapper.queryById(topicId);
                CourseTopic courseTopic = new CourseTopic();
                courseTopic.setCourseId(String.valueOf(course.getCourseId()));
                courseTopic.setTopicId(topicId);
                courseTopic.setTopicType(sysTopic.getTopicType());
                list.add(courseTopic);
            }
            courseTopicMapper.batchCourseTopic(list);
        }

    }

}
